package com.ibiaoke.marketing.service;

import com.ibiaoke.common.base.Page;
import com.ibiaoke.common.exception.ServiceException;
import com.ibiaoke.common.utils.AssertUtils;
import com.ibiaoke.marketing.dao.CouponDao;
import com.ibiaoke.marketing.entity.Coupon;
import com.ibiaoke.marketing.mapper.CouponMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by huhaichao on 2017/6/14.
 */

@Service
public class CouponServiceImpl implements CouponService{

    @Autowired
    private CouponDao couponDao;

    @Override
    public int saveCoupon(Coupon coupon) {
        if(coupon.getScopeType()==null){
            throw new ServiceException("优惠券范围类型不能为空");
        }
        if(coupon.getPriceType()==null){
            throw new ServiceException("优惠券金额类型不能为空");
        }
        if(coupon.getReceiveBeginTime()==null||coupon.getReceiveEndTime()==null){
            throw new ServiceException("优惠券领取时间范围不能为空");
        }
        if(coupon.getFullPrice()==null){
            throw new ServiceException("优惠券满金额不能为空");
        }
        if(coupon.getCouponPrice()==null){
            throw new ServiceException("优惠券金额不能为空");
        }
        if(coupon.getTotalCount()==null){
            throw new ServiceException("优惠券总数不能为空");
        }
        coupon.setCouponCode(UUID.randomUUID().toString());
        coupon.setReceivedCount(0);
        coupon.setDelStatus((byte) 0);
        coupon.setUpdateTime(new Date());
        coupon.setCreateTime(new Date());
        return couponDao.insertSelective(coupon);
    }

    @Override
    public int updateCoupon(Coupon coupon) {
        if(coupon.getCouponId()==null){
            throw new ServiceException("优惠券id不能为空");
        }
        coupon.setUpdateTime(new Date());
        return couponDao.updateByPrimaryKeySelective(coupon);
    }

    @Override
    public int deleteCoupon(Integer couponId) {
        if(couponId==null){
            throw new ServiceException("优惠券id不能为空");
        }
        return couponDao.deleteCoupon(couponId);
    }

    @Override
    public List<Coupon> listCoupon(String couponName, Integer useType, Date beginTime, Date endTime, Page page) {
        return couponDao.listCoupon(couponName, useType, beginTime, endTime, page);
    }

    @Override
    public Coupon getCoupon(Integer couponId) {
        if(couponId==null){
            throw new ServiceException("优惠券id不能为空");
        }
        return couponDao.selectByPrimaryKey(couponId);
    }

    @Override
    public Coupon getCoupon(String couponCode) {
        AssertUtils.notBlank(couponCode,"优惠券编码不能为空");
        return couponDao.getCoupon(couponCode);
    }

    @Override
    public int updateCouponNum(Integer couponId) {
        Coupon coupon = couponDao.selectByPrimaryKey(couponId);
        coupon.setReceivedCount(coupon.getReceivedCount() + 1);
        return couponDao.updateByPrimaryKeySelective(coupon);
    }
}
